<?php
session_start();
require_once "includes/config.php";
$i_product_id = $_REQUEST['product_id'];
$item_code = $_REQUEST['item_code'];

if($i_product_id!=0 || $i_product_id!='')
{
  $getPid=mysql_fetch_array(mysql_query("select * from tbl_items where product_id='".$i_product_id."'"));
  $product_id=$getPid['item_id'];
  $tran_qry="select * from tbl_orders where item_id='$product_id'";
  $qry = mysql_query("select * from tbl_orders where item_id='$product_id'") or die(mysql_error());
}
else
{
  $qry = mysql_query("select * from tbl_orders where item_code='$item_code'") or die(mysql_error());
}
$cnt = mysql_num_rows($qry);
if($cnt>0)
{
  $orders = mysql_fetch_array($qry);
  $item_name = $orders['item_name'];
  $ordered_by = $orders['ordered_by'];
  $sql = mysql_query("select first_name,last_name from tbl_users where user_id=$ordered_by") or die(mysql_error());
  $c_sql = mysql_num_rows($sql);
  if($c_sql>0)
  {
    $user = mysql_fetch_array($sql);
    $first_name = $user['first_name'];
    $last_name = $user['last_name'];
  }
  else
  {
    $first_name = '';
    $last_name = '';
  }
  
  if(isset($_POST['shipped_date']) && $_POST['shipped_date']!='')
  {
    $curr_date = $_POST['shipped_date'];
  }
  else
  {
    $curr_date = date('Y-m-d');
  }
  if($item_code!="")
  {
  $qry1 = mysql_query("update tbl_orders set shipped_date='".$curr_date."', shipped_verified=1, verify_code='$item_code' where item_id='$product_id'") or die(mysql_error());
  }
  else
  {
  $qry1 = mysql_query("update tbl_orders set shipped_date='".$curr_date."', shipped_verified=1 where item_id='$product_id'") or die(mysql_error());
  }
  if($qry1)
  {
	 if($product_id!=0 || $product_id!='')
     {
		 $selProduct=mysql_fetch_array(mysql_query("select * from tbl_product where product_id='".$product_id."'"));
		 
		 $subject="Your Shipping is Confirmed";
		 $sqlEmail="select * from tbl_emails where email_type='Shipping Confirmed'";
		 $resEmail=mysql_query($sqlEmail);
		 $rowEmail=mysql_fetch_array($resEmail);
		 
		 
		 $selUser=mysql_fetch_array(mysql_query("select * from tbl_users where user_id='".$_SESSION['user_id_sess']."'"));
		 $fullname=$selUser['first_name']." ".$selUser['last_name'];
		 
		 $message=str_replace('%fname%',$selUser['first_name'],str_replace('%lname%',$selUser['last_name'],str_replace('%pname%',stripslashes($orders['item_name']),str_replace('%price%',stripslashes($selProduct['product_price']),$rowEmail['email_body']))));
		 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
		 $headers .= 'MIME-Version: 1.0' . "\r\n";
		 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		 mail($selUser['email'],$subject, $message, $headers);
	 }
     echo $curr_date."###".$first_name."###".$last_name."###".$item_name."###".$item_code;
  }
  else
    echo 'fail';
}
else
{
  echo 'fail';
}

?>

